home *** CD-ROM | disk | FTP | other *** search
/ Trading on the Edge / Trading On The Edge - CD-ROM Toolkit (Wayzata Technology)(2031)(1994).bin / mac / Mac_Files / Software Utilities / NN PreProcessing / FINRTN.C < prev    next >
C/C++ Source or Header  |  1992-09-20  |  11KB  |  336 lines

  1. /* 05-Jul-92  (finrtn.c)  Financial Routines in "C" */
  2.  
  3. #include <math.h>
  4.  
  5. /************************************************************************
  6.  * Copyright(C) 1992 High-Tech Communications.                          *
  7.  * 103 Buckskin Court, Sewickley, PA 15143                              *
  8.  *                                                                      *
  9.  * Written by Casimir C. Klimasauskas                                   *
  10.  *                                                                      *
  11.  * All rights reserved.  No part of this program may be reproduced,     *
  12.  * stored in a retrieval system, or tramsmitted, in any form or by any  *
  13.  * means, electronic, mechanical, photocopying, recording or otherwise  *
  14.  * without the prior written permission of the copyright owner,         *
  15.  * High-Tech Communications.                                            *
  16.  *                                                                      *
  17.  * These programs are supplied on an "as-is" basis with no warranties   *
  18.  * of fitness or operability, either express or implied.                *
  19.  *                                                                      *
  20.  ************************************************************************
  21.  */
  22.  
  23. #define    FAST    register    /* for machines with lots of registers */
  24.  
  25. static char Copyright[] = {"Copyright (C) 1992, High-Tech Communications."};
  26.  
  27. /* --- MovAvgV() Computes moving average for one averaging period
  28.  */
  29.  
  30. void MovAvgV( rvecPD, vecPD, veclI, nI )
  31. float    *rvecPD;    /* (r) return vector */
  32. float    *vecPD;        /* (i) pointer to start of data vector */
  33. int     veclI;        /* (i) length of input (output) vector */
  34. int     nI;        /* (i) number of items to average over */
  35. {
  36.     FAST double     r;    /* result */
  37.     FAST float    *vP;    /* pointer to data */
  38.     FAST int     aI;    /* alternate index */
  39.     FAST int     wI;    /* work index */
  40.     FAST int     cI;    /* counter */
  41.  
  42.     if ( nI <= 0 || veclI <= 0 ) return;
  43.  
  44.     for( wI = 0, vP = vecPD; wI < veclI; wI++ ) {
  45.     aI = (wI < nI)? 0:(wI-nI+1);
  46.     for( r = 0., cI = 0; aI <= wI; aI++, cI++ )
  47.         r += vP[aI];
  48.     rvecPD[wI] = r / cI;
  49.     }
  50.     return;
  51. }
  52.  
  53.  
  54. /* --- RSIV() Relative Strength Index
  55.  */
  56.  
  57. void RSIV( rvecPD, vecPD, veclI, nI )
  58. float    *rvecPD;    /* (r) return vector */
  59. float    *vecPD;        /* (i) pointer to start of data vector */
  60. int     veclI;        /* (i) length of input (output) vector */
  61. int     nI;        /* (i) number of items to compute rsi over */
  62. {
  63.     FAST double     r;    /* result */
  64.     FAST float    *vP;    /* pointer to data */
  65.     FAST int     wI;    /* work index */
  66.     FAST int     aI;    /* alternate work index */
  67.     double     lnz;    /* last non-zero RSI */
  68.     double     AvUpD;    /* average of "ups" */
  69.     double     AvDnD;    /* average of "downs" */
  70.  
  71.     if ( nI <= 0 || veclI <= 0 ) return;
  72.  
  73.     rvecPD[0] = 0.0;
  74.     lnz   = 0.;
  75.     for( wI = 1, vP = vecPD; wI < veclI; wI++ ) {
  76.     aI = (wI < nI)? 1:(wI-nI+1);
  77.     AvUpD = AvDnD = 0.;
  78.     for( ; aI <= wI; aI++ ) {
  79.        r = vP[aI] - vP[aI-1];
  80.        if (      r > 0.0 ) { AvUpD += r; }
  81.        else if ( r < 0.0 ) { AvDnD -= r; }
  82.     }
  83.  
  84.     if ( AvDnD <= 0. && AvUpD <= 0. )
  85.         r = lnz;                /* both zero */
  86.     else if ( AvDnD <= 0. ) r = 100.;    /* all ups */
  87.     else if ( AvUpD <= 0. ) r = 0.;        /* all downs */
  88.         else r = 100.-100./(1.+AvUpD/AvDnD);    /* both */
  89.  
  90.     if ( r < 0.001 ) r = 0.;
  91.     else         lnz = r;
  92.         rvecPD[wI] = r;
  93.     }
  94.     return;
  95. }
  96.  
  97. /* --- StochasticV() Compute the stochastic function
  98.  */
  99.  
  100. void StochasticV( rvecPD, vecPD, veclI, nI, sI )
  101. float    *rvecPD;    /* (r) return vector */
  102. float    *vecPD;        /* (i) pointer to start of data vector */
  103. int     veclI;        /* (i) length of input (output) vector */
  104. int     nI;        /* (i) length of period to compute over */
  105. int     sI;        /* (i) number of periods to "smooth" over */
  106. {
  107.     FAST int     aI;    /* work index */
  108.     FAST double     Mn,Mx;    /* min, max */
  109.     FAST double     r;    /* result */
  110.     int          wI;    /* work index */
  111.     int         cI;    /* count */
  112.  
  113.     /* --- compute the raw stochastic function --- */
  114.  
  115.     for( wI = 0; wI < veclI; wI++ ) {
  116.     aI = (wI < nI )? 0:(wI-nI+1);
  117.     Mn = Mx = vecPD[aI++];
  118.     for(; aI <= wI; aI++ ) {
  119.         r = vecPD[aI];
  120.         if ( r < Mn ) Mn = r;
  121.         if ( r > Mx ) Mx = r;
  122.     }
  123.     if ( (Mx - Mn) < 1.e-6 ) r = 0.;
  124.     else             r = (vecPD[wI] - Mn) / (Mx - Mn);
  125.     rvecPD[wI] = r;
  126.     }
  127.     if ( sI <= 1 )
  128.     return;            /* no smoothing required */
  129.  
  130.     /* --- smooth the function over the requested periods --- */
  131.  
  132.     for( wI = veclI-1; wI >= 0; wI-- ) {
  133.     aI = (wI < sI)? 0:(wI-sI+1);
  134.     for( r = 0., cI = 0; aI <= wI; aI++, cI++ )
  135.         r += rvecPD[aI];
  136.     rvecPD[wI] = r / cI;
  137.     }
  138.     return;
  139. }
  140.  
  141.  
  142. /* --- VolatilityV() Raw volatility in price over a period of time
  143.  */
  144.  
  145. void VolatilityV(  rvecPD, vecPD, veclI, nI )
  146. float    *rvecPD;    /* (r) return vector */
  147. float    *vecPD;        /* (i) pointer to start of data vector */
  148. int     veclI;        /* (i) length of input (output) vector */
  149. int     nI;        /* (i) length of period to compute over */
  150. {
  151.     FAST int     aI;    /* work index */
  152.     FAST double     r;    /* result */
  153.     int          wI;    /* work index */
  154.  
  155.     for( wI = 0; wI < veclI; wI++ ) {
  156.     aI = (wI < nI)? 0:(wI-nI+1);
  157.     r  = vecPD[wI] - vecPD[aI];    /* change in price */
  158.     if ( r < 0. ) r = 0.-r;        /* take absolute value */
  159.     rvecPD[wI] = r;
  160.     }
  161.     return;
  162. }
  163.  
  164. /* --- HistoricalVolatilityD() Volatility annualized over an entire data set
  165.  */
  166.  
  167. float HistoricalVolatilityD( vecPD, veclI )
  168. float    *vecPD;        /* (i) pointer to start of data vector */
  169. int     veclI;        /* (i) length of input vector */
  170. {
  171.     double     r;        /* return value */
  172.     double     AvgLogD;    /* average change */
  173.     double     CumDevD;    /* cumulative deviations */
  174.     double     r1, r2;    /* work values */
  175.     int         wI;        /* work index */
  176.  
  177.     if ( veclI < 2 ) return( 0. );
  178.  
  179.     for( AvgLogD = 0., wI = 1; wI < veclI; wI++ )
  180.     if ( (r1 = vecPD[wI]) > 0. && (r2 = vecPD[wI-1]) > 0. )
  181.         AvgLogD += log( r1 ) - log( r2 );
  182.     AvgLogD /= (veclI-1);
  183.  
  184.     for( CumDevD = 0., wI = 1; wI < veclI; wI++ ) {
  185.     if ( (r1=vecPD[wI]) > 0. && (r2=vecPD[wI-1]) > 0. ) {
  186.         r = (log( r1 ) - log( r2 )) - AvgLogD;
  187.         CumDevD += (r * r);
  188.     }
  189.     }
  190.     r = sqrt( (CumDevD / (veclI-2) ) * 250. );
  191.     return( r );
  192. }
  193.  
  194. /* --- HighLowVolatilityV() High/Low Volatility
  195.  */
  196.  
  197. void HighLowVolatilityV(  rvecPD, highPD, lowPD, veclI, nI )
  198. float    *rvecPD;    /* (r) return vector */
  199. float    *highPD;    /* (i) high prices */
  200. float    *lowPD;        /* (i) low prices */
  201. int     veclI;        /* (i) length of input (output) vector */
  202. int     nI;        /* (i) length of period to compute over */
  203. {
  204.     FAST int     aI;    /* work index */
  205.     FAST double     r,s;    /* result */
  206.     double     r1,r2;    /* work reals */
  207.     int          wI;    /* work index */
  208.  
  209.     for( wI = 0; wI < veclI; wI++ ) {
  210.     if ( (wI+1) < nI ) {
  211.         rvecPD[wI] = 0.;
  212.     } else {
  213.         for( r = 0., aI = wI-nI+1; aI <= wI; aI++ ) {
  214.         if ( (r1=highPD[aI]) > 0. && (r2=lowPD[aI]) > 0. ) {
  215.             s = log( r1 ) - log( r2 );
  216.             r += (s * s);
  217.         }
  218.         }
  219.         rvecPD[wI] = sqrt( (r * 0.361)/nI );
  220.     }
  221.     }
  222.     return;
  223. }
  224.  
  225. /* --- SlidingRegressionV() Sliding regression coefficients.
  226.  */
  227.  
  228. void SlidingRegressionV( slopePD, interceptPD, refPD, vecPD, veclI, nI )
  229. float    *slopePD;    /* (r) Slope of line */
  230. float    *interceptPD;    /* (r) Intercept of line */
  231. float    *refPD;        /* (i) reference (x) vector */
  232. float    *vecPD;        /* (i) data vector */
  233. int     veclI;        /* (i) length of data vector */
  234. int     nI;        /* (i) number of days to compute slope over */
  235. {
  236.     double     Sx,Sxx, Sy,Syy, Sxy;    /* sums for regression */
  237.     double     num, den;        /* numerator & denominator */
  238.     double     rx, ry;        /* work reals */
  239.     int         wI;            /* main work index */
  240.     int         aI;            /* subsidiary work index */
  241.     int         lnI;            /* local number of items */
  242.     int         i;            /* work integer */
  243.     /* NOTE: 20-Sep-92 cck: Use modified algorithm which demonstrates
  244.      *   much better stability when Average of X or Y are much different
  245.      *   than zero.
  246.      */
  247.  
  248.     for( wI = 0; wI < veclI; wI++ ) {
  249.     aI = (wI < nI)? 0:(wI-nI+1);    /* start index */
  250. #if 1
  251.     Sx = Sxx = Sy = Syy = Sxy = 0.;
  252.     i = aI;                /* save start index */
  253.     for( lnI=0 ; aI <= wI; aI++, lnI++ ) {
  254.         rx   = refPD? refPD[aI]:aI;    /* "x" value */
  255.         ry   = vecPD[aI];        /* "y" value */
  256.         Sx  += rx;            /* sum of x-values */
  257.         Sy  += ry;            /* sum of y-values */
  258.     }
  259.     Sx /= (double)(lnI);        /* x-bar (average of x) */
  260.     Sy /= (double)(lnI);        /* y-bar (average of y) */
  261.     for( aI = i; aI <= wI; aI++ ) {
  262.         rx   = refPD? refPD[aI]:aI;    /* "x" value */
  263.         ry   = vecPD[aI];        /* "y" value */
  264.         den  = (rx - Sx);        /* (x-xbar) */
  265.         Sxy += den * (ry - Sy);    /* (x-xbar)*(y-ybar) */
  266.         Sxx += den * den;        /* (x-xbar)*(x-xbar) */
  267.     }
  268.     den = Sxy / (Sxx > (1.e-10)? Sxx:(1.e-10));    /* slope */
  269.     num = Sy - den * Sx;                /* intercept */
  270.     if ( slopePD )        slopePD[wI]     = den;
  271.     if ( interceptPD )  interceptPD[wI] = num;
  272. #else
  273.     Sx = Sxx = Sy = Syy = Sxy = 0.;
  274.     for( lnI=0 ; aI <= wI; aI++, lnI++ ) {
  275.         rx   = refPD? refPD[aI]:aI;    /* "x" value */
  276.         ry   = vecPD[aI];        /* "y" value */
  277.         Sx  += rx;        Sxx += rx * rx;
  278.         Sy  += ry;        Syy += ry * ry;
  279.         Sxy += rx * ry;
  280.     }
  281.     den = ( lnI * Sxx - Sx * Sx );
  282.     if ( fabs( den ) < 1.e-4 ) den = 0.;
  283.     else               den = 1.0/den;
  284.     if ( slopePD )
  285.         slopePD[wI]     = (lnI * Sxy - Sx * Sy) * den;
  286.     if ( interceptPD )
  287.         interceptPD[wI] = (Sxx * Sy - Sx * Sxy) * den;
  288. #endif
  289.     }
  290.     return;
  291. }
  292.  
  293.  
  294. /* --- RollingCorrelationV() Rolling Correlation coefficients.
  295.  */
  296.  
  297. void RollingCorrelationV( corrPD, refPD, vecPD, veclI, nI )
  298. float    *corrPD;    /* (r) Correlation Coefficient */
  299. float    *refPD;        /* (i) reference (x) vector */
  300. float    *vecPD;        /* (i) data vector */
  301. int     veclI;        /* (i) length of data vector */
  302. int     nI;        /* (i) number of days to compute correlation over */
  303. {
  304.     double     Sx,Sxx, Sy,Syy, Sxy;    /* sums for correlation */
  305.     double     den;            /* denominator */
  306.     double     rx, ry;        /* work reals */
  307.     int         wI;            /* main work index */
  308.     int         aI;            /* subsidiary work index */
  309.     int         lnI;            /* local number of items */
  310.  
  311.     for( wI = 0; wI < veclI; wI++ ) {
  312.     aI = (wI < nI)? 0:(wI-nI+1);    /* start index */
  313.     Sx = Sxx = Sy = Syy = Sxy = 0.;
  314.     for( lnI=0; aI <= wI; aI++, lnI++ ) {
  315.         rx   = refPD? refPD[aI]:aI;    /* "x" value */
  316.         ry   = vecPD[aI];        /* "y" value */
  317.         Sx  += rx;
  318.         Sy  += ry;
  319.     }
  320.     Sx /= lnI;    /* x-bar */
  321.     Sy /= lnI;    /* y-bar */
  322.     aI = (wI < nI)? 0:(wI-nI+1);    /* start index */
  323.     for( ; aI <= wI; aI++ ) {
  324.         rx   = refPD? refPD[aI]:aI;    /* "x" value */
  325.         ry   = vecPD[aI];        /* "y" value */
  326.         Sxx += (rx - Sx) * (rx - Sx );
  327.         Syy += (ry - Sy) * (ry - Sy );
  328.         Sxy += (rx - Sx) * (ry - Sy );
  329.     }
  330.     den = sqrt( Sxx * Syy );
  331.     if ( den < 1.e-6 )    corrPD[wI] = 0.0;
  332.     else            corrPD[wI] = Sxy / den;
  333.     }
  334.     return;
  335. }
  336.